package org.vse.aha.gui.windows;

import java.util.Collection;
import java.util.Iterator;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
import org.vse.aha.common.quotes.Quote;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.vse.aha.gui.windows//QuoteDetail//EN",
autostore = false)
@TopComponent.Description(preferredID = "QuoteDetailTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@TopComponent.OpenActionRegistration(displayName = "#CTL_QuoteDetailTopComponent",
preferredID = "QuoteDetailTopComponent")
@Messages({
  "CTL_QuoteDetailAction=DetailVyroku",
  "CTL_QuoteDetailTopComponent=DetailVyroku Window",
  "HINT_QuoteDetailTopComponent=This is a DetailVyroku window"
})
public final class QuoteDetailTopComponent extends TopComponent implements LookupListener {
  private Lookup.Result<Quote> result = null;
  
  public QuoteDetailTopComponent() {
    initComponents();
    setName(Bundle.CTL_QuoteDetailTopComponent());
    setToolTipText(Bundle.HINT_QuoteDetailTopComponent());

  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextAutor = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaText = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(QuoteDetailTopComponent.class, "QuoteDetailTopComponent.jLabel1.text")); // NOI18N

        jTextAutor.setText(org.openide.util.NbBundle.getMessage(QuoteDetailTopComponent.class, "QuoteDetailTopComponent.jTextAutor.text")); // NOI18N

        jTextAreaText.setColumns(20);
        jTextAreaText.setRows(5);
        jTextAreaText.setText(org.openide.util.NbBundle.getMessage(QuoteDetailTopComponent.class, "QuoteDetailTopComponent.jTextAreaText.text")); // NOI18N
        jScrollPane1.setViewportView(jTextAreaText);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(QuoteDetailTopComponent.class, "QuoteDetailTopComponent.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextAutor))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextAutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaText;
    private javax.swing.JTextField jTextAutor;
    // End of variables declaration//GEN-END:variables
  @Override
  public void componentOpened() {
    result = Utilities.actionsGlobalContext().lookupResult(Quote.class);
    result.addLookupListener(this);
    
    aktualizovatForm();
  }

  @Override
  public void componentClosed() {
    result.removeLookupListener(this);
  }
 
  void writeProperties(java.util.Properties p) {
    // better to version settings since initial version as advocated at
    // http://wiki.apidesign.org/wiki/PropertyFiles
    p.setProperty("version", "1.0");
    // TODO store your settings
  }

  void readProperties(java.util.Properties p) {
    String version = p.getProperty("version");
    // TODO read your settings according to their version
  }

  @Override
  public void resultChanged(LookupEvent ev) {
    aktualizovatForm();
  }
  
  private void aktualizovatForm() {
    Collection<? extends Quote> uzly = result.allInstances();
    Iterator<? extends Quote> it1 = uzly.iterator();
    while (it1.hasNext()) {
      Quote výrok = (Quote) it1.next();
      this.jTextAreaText.setText(výrok.getTextVýroku());
      this.jTextAutor.setText(výrok.getAutor());
    }
  }
}
